<?php
namespace app\admin\controller;
use think\Controller;
use sand\handle;
use app\admin\model\Collection as CollectionModel;
use app\admin\controller\Base;
/**
* 
*/
class Collection extends Base
{
    public function index(){
        $map=[];
        if(input('param.id')){
            $map['id']=intval(input('param.id'));
        }
        $orderBy=['status'=>'ASC','collect_time'=>'DESC','deadline'=>'ASC','id'=>'ASC'];
    	$this->assign('collections',model("Collection")->where($map)->order($orderBy)->paginate(10));
        return $this->fetch();
    }

    public function collect(){
    	$id=intval(input('param.id'));

    	$collection=model('Collection')->get($id);
    	$userAcc=model('user_account')->get(['uid'=>$collection->uid]);
    	$this->assign('userAcc',$userAcc);
    	$this->assign('collection',$collection);
    	return $this->fetch();
    }
    public function doclose(){
        $id=intval(input('param.id'));
        $collection=model('Collection')->get($id);
        if($collection->status)
            $this->error("该纪录无法关闭");
        $collection->status=2;
        if($collection->save())
            $this->success("操作成功");
        else $this->success("操作失败");
    }
    public function docollect(){
        $id=intval(input('param.id'));
        $collection=model('Collection')->get($id);
        $userAcc=model('user_account')->get(['uid'=>$collection->uid])->getData();
        $productId='00000002';
        $params = require_once CONF_PATH.'sand/config/params.php';
        $collectionData=$params['Collection'];
        $collectionData['pt']['productId']=$productId;
       
        $collectionData['pt']['tranAmt']=str_pad($collection->amount*100,12,"0",STR_PAD_LEFT);
       // dump($AgentPay['pt']['tranAmt']);
        $collectionData['pt']['accAttr']=$userAcc['acc_attr'];
        $collectionData['pt']['accType']=$userAcc['acc_type'];
        $collectionData['pt']['accNo']=$userAcc['acc_no'];
        $collectionData['pt']['accName']=$userAcc['acc_name'];
       
        $collectionData['pt']['certType']=$userAcc['cert_type'];
        $collectionData['pt']['certNo']=$userAcc['cert_no'];
        $collectionData['pt']['provNo']=$userAcc['prov_no'];
        $collectionData['pt']['phone']=$userAcc['phone'];
        $collectionData['pt']['cardId']=model('user')->where(['id'=>$collection->uid])->value('idcard');
        $cert = require_once CONF_PATH.'sand/config/cert.php';
        $handle = new handle($cert);
        /* exit();*/
        $res=json_decode($handle->execute($collectionData));
        $order=[
            'order_code'=>$res->orderCode,
            'product_id'=>$productId,
            'tran_time'=>$res->tranTime,
            'status'=>1,
        ];
        model('order')->data($order)->save();
        if($res->respCode == '0000'&&$res->resultFlag == '0'){
            $collection->collect_time = time();
            $collection->status = 1;
            if(model('order')->saveByRes($res)&&$collection->save())//保存订单更新代扣
                $this->success("操作成功");
            else $this->error("操作失败");
        }else{
            dump($collectionData);
            dump($res);
            //$this->error($res->respCode.$res->respDesc);
        }
    }

    public function add(){
        $uid=input('param.uid');
        if($uid){
            $user=model('user')->field("id,name,idcard,phone")->find(intval($uid));
            if($user)
                $this->assign('user',$user);
            else $this->error("用户不存在");
        }
        return $this->fetch();
    }
    public function doadd(){
        $uid=intval($_POST['uid']);
        if(!$uid||!model('user_account')->get(['uid'=>$uid]))
            $this->error("用户未认证");
        $validate=validate('Collection');
        if(!$validate->scene('add')->check(input('post.')))
            $this->error($validate->getError()); 
        $amount=floatval(input('post.amount'));        
        $periods = input('post.periods');
        $periods =$periods?$periods:1;
        $deadline=input('post.deadline'); 
        for($i = 0;$i<$periods;$i++){          
            $dead_line = strtotime($deadline." +".$i." month");
            $data[$i] = array('amount'=>$amount,'uid' =>$uid,'create_time'=>time(),'deadline'=>$dead_line);          
        }
         $collect = new CollectionModel();
        // $collect->uid =$uid;
        // $collect->amount =$amount;
        // $collect->deadline =$deadline;

        // $collect->create_time =time();
        if(model('Collection')->saveAll($data))
            // $this->success("增加成功",url('collect',['id'=>$collect->id],''));
            $this->success("增加成功");
        else
            $this->error("增加失败");
    }
     public function verify(){
        $phone = $_POST['phone'];
        $user = model('user')->get(['phone'=>$phone]);
        if(!$user) exit(json_encode(['res_code'=>1,'res_msg'=>'用户不存在']));
        $verify = model('user_account')->get(['uid'=>$user['id']]);
        if($verify){
            $json_verify['res_data'] = $user;
            $json_verify['res_code']=0;
        }else{
            $json_verify['res_code']=1;
            $json_verify['res_msg']="用户未认证";
        }
        $json_verify = json_encode($json_verify);
        echo $json_verify;
    }
}
